<template>
  <div class="app-container">
    <!-- 字典列表 -->
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="10" :lg="11" :xl="11" style="margin-bottom: 10px">
        <el-card class="box-card">
          <!--工具栏-->
          <div class="head-container">
            <div v-if="searchToggle">
              <!-- 搜索 -->
              <el-input v-model="query.keyword" clearable size="small" placeholder="输入名称或者描述搜索" style="width: 200px;" class="filter-item" @keyup.enter="fetchList" />
              <rr-operation />
            </div>
            <crud-operation />
          </div>
          <!--表格渲染-->
          <el-table ref="table" v-loading="tableLoading" :data="tableData" highlight-current-row style="width: 100%;" @selection-change="selectionChangeHandler" @current-change="handleCurrentChange">
            <el-table-column type="selection" width="55" />
            <el-table-column :show-overflow-tooltip="true" prop="name" label="名称" />
            <el-table-column :show-overflow-tooltip="true" prop="description" label="描述" />
            <el-table-column v-permission="[].concat(permission.edit, permission.remove)" label="操作" width="130px" align="center" fixed="right">
              <template v-slot="{row}">
                <ud-operation
                  :data="row"
                />
              </template>
            </el-table-column>
          </el-table>
          <!--分页组件-->
          <pagination />

          <!--表单组件-->
          <el-dialog :close-on-click-modal="false" v-model="dialog.visible" :title="dialog.title" width="500px">
            <el-form ref="formRef" :model="form" :rules="rules" size="small" label-width="80px">
              <el-form-item label="字典名称" prop="name">
                <el-input v-model="form.name" style="width: 370px;" />
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="form.description" style="width: 370px;" />
              </el-form-item>
            </el-form>
            <template v-slot:footer class="dialog-footer">
              <el-button @click="dialog.visible = false">关闭</el-button>
              <el-button :loading="dialog.status === DIALOG_STATUS.PROCESSING" type="primary" @click="submitCE">确认</el-button>
            </template>
          </el-dialog>

        </el-card>
      </el-col>
      <!-- 字典详情列表 -->
      <el-col :xs="24" :sm="24" :md="14" :lg="13" :xl="13">
        <el-card class="box-card">
          <template v-slot:header class="clearfix">
            <span>字典详情</span>
            <el-button v-permission="permission.create"
              v-if="detailCreateButton"
              class="filter-item"
              size="small"
              style="float: right;padding: 4px 10px"
              type="primary"
              icon="plus"
              @click="$refs.dictDetail && $refs.dictDetail.handleCreate()"
            >新增</el-button>
          </template>
          <dictDetail ref="dictDetail" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>

import dictDetail from './dictDetail'
import dictApi from '@/api/system/dict'

import crudOperation from '@crud/CRUD.operation'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import useCrud from "@crud/useCrud"


export default {
  name: 'Dict',
  components: { crudOperation, pagination, rrOperation, udOperation, dictDetail },
  setup() {
    return {
      permission: {
        create: ['dict:add'],
        edit: ['dict:edit'],
        remove: ['dict:del']
      },
      ...useCrud({
        bottomOffset: false,
        title: "字典",
        defaultForm: { id: null, name: null, description: null, dictDetails: [] },
        methods: {...dictApi},
        optShow: {
          download: false
        }
      })
    }
  },
  data() {
    return {
      detailCreateButton: false,
      queryTypeOptions: [
        { key: 'name', display_name: '字典名称' },
        { key: 'description', display_name: '描述' }
      ],
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 选中字典后，设置字典详情数据
    handleCurrentChange(val) {
      if (val) {
        this.detailCreateButton = true
        this.$refs.dictDetail.query.dictName = val.name
        this.$refs.dictDetail.dictId = val.id
        this.$refs.dictDetail.fetchList()
      }
    }
  }
}
</script>

<style scoped>
</style>
